package com.five.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * 文件同意下载控制器
 * @author Administrator
 *
 */
@RequestMapping("/download")
@Controller
@Api(tags = "统一文件下载接口")
@CrossOrigin
public class DownloadController {


	private String uploadVideoPath = "D:\\qf\\data\\video\\";//视频保存位置

	private String uploadImgPath = "D:\\qf\\data\\img\\";//图片保存位置
	
	/**
	 * 视频文件下载方法
	 * @param filename
	 * @param response
	 */
	@RequestMapping(value = "/download_video",method = RequestMethod.GET)
	@ApiOperation("视频下载接口")
	public void downloadVideoFile(@RequestParam(name="filename",required=true)String filename, HttpServletResponse response, HttpServletRequest request){
		filename = uploadVideoPath + filename;
		File file = new File(filename);
		//设置返回文件大小
		response.setContentLength(Integer.valueOf(file.length()+""));
		//设置返回类型
		try {
			response.setContentType(Files.probeContentType(Paths.get(filename)));
		} catch (Exception e) {
			e.printStackTrace();
		}
		String rangeString = request.getHeader("Range");//如果是video标签发起的请求就不会为null
		long range = Long.valueOf(rangeString.substring(rangeString.indexOf("=") + 1, rangeString.indexOf("-")));
		//拖动进度条时的断点，其中10000是上面的视频文件大小，改成你的就好
		response.setHeader("Content-Range", String.valueOf(range + (file.length()-1)));
		response.setHeader("Accept-Ranges", "bytes");
		String suffix = filename.substring(filename.lastIndexOf(".")+1,filename.length());
		response.setHeader("Content-Disposition", "attachment;filename=" + System.currentTimeMillis() + suffix);
		writeFileToResponse(file,response);
	}


	@RequestMapping(value = "download_img",method = RequestMethod.GET)
	@ApiOperation("图片下载接口")
	public void downloadImgFile(@RequestParam(name="filename",required=true)String filename,HttpServletRequest request, HttpServletResponse response){
		filename = uploadImgPath + filename;
		File file = new File(filename);
		//设置返回文件大小
		response.setContentLength(Integer.valueOf(file.length()+""));
		//设置返回类型
		try {
			response.setContentType(Files.probeContentType(Paths.get(filename)));
		} catch (Exception e) {
			e.printStackTrace();
		}
		String suffix = filename.substring(filename.lastIndexOf(".")+1,filename.length());
		response.setHeader("Content-Disposition", "attachment;filename=" + System.currentTimeMillis()+"."+suffix);
		writeFileToResponse(file,response);
	}
	
	/**
	 * 将文件流读出放入至指定的response
	 * @param file
	 * @param response
	 */
	private void writeFileToResponse(File file,HttpServletResponse response){
		byte[] buff = new byte[1024];
		FileInputStream fis = null;
		BufferedInputStream bis = null;
		try {
			fis = new FileInputStream(file);
			bis = new BufferedInputStream(fis);
			ServletOutputStream outputStream = response.getOutputStream();
			int i = bis.read(buff);
			while(i != -1){
				try {
					outputStream.write(buff, 0, i);
					i = bis.read(buff);
				} catch (Exception e) {
					// TODO: handle exception
				}				
			}
			bis.close();
			fis.close();
		} catch (Exception e) {

		}


	}


}
